package com.zhangwoo.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;

import com.meiya.lab.algorithm.ahocorasick.AhoCorasick;
import com.meiya.lab.algorithm.ahocorasick.SearchResult;
import com.zhangwoo.analyser.jobs.matcher.AllDataAnalyser;

public class AllDataAnalyserTest {
	private static Logger logger = Logger.getLogger(AllDataAnalyserTest.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		args=new String[]{"D:\\TestZone\\tt.txt"};
		String fpath = args[0];
		String testHtml = JavaUtil.readFile(fpath);

		AhoCorasick tree = new AhoCorasick();
		String text = testHtml;

		String t_info = "";
//		String t_notag = text.replaceAll("\\s+", " ") //  yachi应当已经处理掉换行符了
//				.replaceAll("<[hH][eE][aA][dD].*?</[hH][eE][aA][dD]>", "")
//		        .replaceAll("<[sS][tT][yY][lL][eE].*?</[sS][tT][yY][lL][eE]>",
//		                "")
//		        .replaceAll(
//                "<[sS][cC][rR][iI][Pp][Tt].*?</[sS][cC][rR][iI][Pp][Tt]>",
//                "").replaceAll("<[^>]*?>", "").replaceAll("&nbsp;", "") // 去除web空格
//				.replaceAll("<!--.*?-->", "");

		// 1.从文件中读取策略表
		String strategy = "#a01_20120814053100~i9100 and i9300 and 三星#a01_20120815023059~三星 and (i9100 or I9100)#a01_20120815023059_p~电池 or CPU or 内存 or 操控 or 功能 or 操作系统 or 散热 or 声音 or 信号 or 屏幕 or 外观 or 价格 or 配件 or 售后维修服务 or 其它"; // 获取如：1,A
		// and B
		logger.info("strategy " + strategy);

		String allTerms = strategy.toString().replaceAll("#[^~]+~", "\\~\\$\\~").replaceAll("\\s+", " ")
		// 所有关键词拼接
		.replace(" and ", "~$~").replace(" or ", "~$~").replace(" not ", "~$~")
		.replace("#", "~$~").replace("(", "~$~").replace(")", "~$~")
		.replace(",", "~$~");
		Logger.getLogger(AllDataAnalyser.class).info("allTerms "+allTerms);
		String[] terms = allTerms.split("\\~\\$\\~");

		for (String xs : terms)
			Logger.getLogger(AllDataAnalyserTest.class).info("terms " + xs);

		// 2.串联所有关键词 AC匹配
		for (int i = 0; i < terms.length; i++) {
			if(terms[i].trim().length()!=0)
				tree.add(terms[i].trim().getBytes(), terms[i].trim());
		}
		tree.prepare();

		Set termsThatHit = new HashSet();
		for (Iterator iter = tree.search(text.getBytes()); iter.hasNext();) {
			SearchResult result = (SearchResult) iter.next();
			termsThatHit.addAll(result.getOutputs());
		}

		if (termsThatHit.size() == 0)
			return;
		logger.info(
				"termsThatHit " + termsThatHit.toString());

		// 3.使用表达式测试是否符合
		Set<String> exps = new HashSet<String>();
		Collections.addAll(exps, strategy.split("#"));
		for (String exp : exps) {
			if (Alarm.isAlerm(("#"+exp).replaceAll("#[^~]+~", ""),
					termsThatHit.toString())) {
				
				String[] expArr=("#"+exp).replaceAll("#[^~]+~", "").replaceAll("\\s+", " ")
				// 所有关键词拼接
				.replace(" and ", "~$~").replace(" or ", "~$~").replace(" not ", "~$~")
				.replace("#", "~$~").replace("(", "~$~").replace(")", "~$~")
				.replace(",", "~$~").split("\\~\\$\\~");
				
				String content=InfoDetails.mainProc(expArr, text);
				if(content.trim().length()!=0)
					logger.info(
							exp + " <~+~> " + t_info + " <~+~> "
									+ content + " <~+~> ");
				logger.info("a");
			}
		}

	}

}
